package com.mall.util;


import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    public static String currentDate() {
        DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        return date.format(new Date(System.currentTimeMillis()));
    }

    public static String currentDatetime() {
        DateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateTime.format(new Date(System.currentTimeMillis()));
    }

    public static Date strToDate(String str) throws ParseException {
        DateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return dateTime.parse(str);
    }

    /**
     * 将当前时间往后推几天
     * @param days 要向后推的天数
     * @return 推后 days 天后的日期字符串，格式为 yyyy-MM-dd
     */
    public static String addDays(int days) {
        DateFormat dateTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, days);
        return dateTime.format(calendar.getTime());
    }

    public static String subDays(int days) {
        DateFormat date = new SimpleDateFormat("yyyy-MM-dd");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH, -(days));
        return date.format(calendar.getTime());
    }

}
